## **BEM** **Created by**: Yandex **Purpose**: BEM provides a structured naming convention to make CSS **scalable**, **predictable**, and **maintainable**. 1. **Block (`.block`)** – A reusable, standalone component (e.g., `button`, `card`, `nav`). ```css .button { display: inline-block; padding: 10px 20px; border-radius: 5px; } ``` 2. **Element (`.block__element`)** – A part of the block that has no meaning without the block (e.g., `button__icon`, `card__title`). ```css .button__icon { margin-right: 5px; } ``` 3. **Modifier (`.block--modifier` or `.block__element--modifier`)** – A variation of the block or element (e.g., `button--primary`, `button--large`). ```css .button--primary { background-color: blue; color: white; } ``` --- ### **Example: BEM in Action** ```html ``` ```css /* Block */ .button { display: inline-block; padding: 10px 20px; border-radius: 5px; } /* Element */ .button__icon { margin-right: 5px; } /* Modifier */ .button--primary { background-color: blue; color: white; } ``` - `.button` → Block (base styles) - `.button__icon` → Element (sub-component) - `.button--primary` → Modifier (variation) ## **SMACSS (Scalable and Modular Architecture for CSS)** **Created by**: Jonathan Snook **Purpose**: SMACSS is a set of guidelines that help categorize and structure CSS in a modular way to improve maintainability and scalability. ### **Key Principles** SMACSS organizes CSS into five main categories: 1. **Base Rules** – Default styles for elements (e.g., ``, ``, `

`, `

`, ``, etc.). ```css body { font-family: Arial, sans-serif; color: #333; } ``` 2. **Layout Rules** – Define the structure of the page (e.g., grid system, major sections like header, sidebar, footer). ```css .layout-grid { display: flex; flex-wrap: wrap; } ``` 3. **Module Rules** – Reusable components like buttons, cards, modals. ```css .card { border: 1px solid #ddd; padding: 16px; border-radius: 5px; } ``` 4. **State Rules** – Define UI states such as active, disabled, hidden. ```css .is-hidden { display: none; } .is-active { background-color: yellow; } ``` 5. **Theme Rules** – Manage styles for branding (colors, typography, themes). ```css .theme-dark { background-color: #222; color: #fff; } ``` ### **Advantages of SMACSS** ✅ Encourages modular CSS ✅ Improves maintainability and reusability ✅ Reduces CSS specificity issues ✅ Works well with CSS preprocessors like SCSS --- ## **OOCSS (Object-Oriented CSS)** **Created by**: Nicole Sullivan **Purpose**: OOCSS promotes reusable and scalable CSS by applying object-oriented programming principles to CSS. ### **Key Principles** 1. **Separation of Structure and Skin** – Separate design (appearance) from the layout (structure). ```css /* Structure */ .box { padding: 20px; border-radius: 5px; } /* Skin */ .box-primary { background-color: blue; color: white; } .box-secondary { background-color: gray; color: black; } ``` An element that needs both the **structure** (`.box`) and the **skin** (`.box-primary`) must have **both classes** applied. For example: ```

This is a primary box.
This is a secondary box.
``` You may have seen this used by Bootstrap! 2. **Separation of Containers and Content** – Avoid styles that are dependent on specific containers. ```css /* Avoid */ .sidebar h2 { font-size: 24px; } /* Better */ .heading-large { font-size: 24px; } ``` ### **Advantages of OOCSS** ✅ Improves CSS reusability ✅ Reduces redundancy in styles ✅ Leads to faster page loads with smaller CSS files ✅ Works well with component-based frameworks (React, Vue, etc.) --- ## **Comparison: SMACSS vs. OOCSS** |Feature|SMACSS|OOCSS| |---|---|---| |**Philosophy**|Categorizes styles into modular sections|Encourages reusable objects in CSS| |**Focus**|Maintainability and scalability|Reusability and performance| |**CSS Specificity**|Lower specificity using structured rules|Lower specificity using reusable objects| |**Implementation Complexity**|Medium|Low| |**Best For**|Large, structured projects|Projects that need highly reusable components| --- ## **How BEM Compares to SMACSS & OOCSS** |Feature|BEM|SMACSS|OOCSS| |---|---|---|---| |**Main Idea**|Naming convention for modularity|Organizes CSS into categories|Encourages reusable objects| |**Naming Structure**|`block__element--modifier`|Uses class categories (base, layout, module, etc.)|Uses structured classes like `box` + `box-primary`| |**CSS Specificity**|Low (flat structure)|Low (modular approach)|Low (encourages reusable objects)| |**Best For**|Teams & large projects needing consistent naming|Large projects needing a structured approach|Reusable UI components across projects| |**Main Benefit**|Clear, predictable class naming|Scalability & maintainability|Reusability & performance| |**Drawback**|Class names can get long|Requires initial structuring|Might require additional classes| --- ## **Can BEM Be Used with SMACSS or OOCSS?** Yes! BEM **focuses on naming**, while SMACSS & OOCSS **focus on architecture**. - ✅ **BEM + SMACSS** → Use SMACSS to organize files and BEM for class names. - ✅ **BEM + OOCSS** → Use OOCSS principles (structure vs. skin) while using BEM for naming. ### **Example: BEM + OOCSS Hybrid** ```html

This is a primary box

``` ```css /* Structure */ .box { padding: 20px; border-radius: 5px; } /* Skin (Modifier) */ .box--primary { background-color: blue; color: white; } /* Element */ .box__text { font-size: 16px; } ``` - **BEM** provides structured class names (`box__text`, `box--primary`). - **OOCSS** keeps structure (`.box`) separate from appearance (`.box--primary`).